package servlet;

import entity.AClass;
import entity.Student;
import service.ClassService;
import service.ClassServiceImpl;
import service.StudentService;
import service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "ClassAddServlet",urlPatterns = "/classAddServlet")
public class ClassAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进来啦。。。。。");
        response.setContentType("text/html; charset=utf-8");

        // 1.获取前台页面发过来的标志位和数据内容
        String flag = request.getParameter("flag");
        //System.out.println("获取到的标志位是：" + flag);
        String className = request.getParameter("className");
        String Grade = request.getParameter("Grade");
        String teacher = request.getParameter("teacher");
        String kouhao=request.getParameter("kouhao");
        String capacity =request.getParameter("capacity");

        // 2.打包数据后通过Service层实现对应的功能
        //System.out.println("数据打包完成！");
        ClassService classService = new ClassServiceImpl();
        // 根据隐藏域的结果分别实现增加和修改的功能
        int res = 0;
        AClass aClass = null;
        if ("add".equals(flag)) {
             aClass= new AClass(className, Grade, teacher, kouhao, capacity);
            res = classService.classAddService(aClass);
            System.out.println("插入数据的结果为：" + res);
        } else {

            aClass = new AClass(flag, Grade, teacher, kouhao, capacity);
            res = classService.classAddService(aClass);
            System.out.println("修改数据的结果为：" + res);
        }

        // 3.通过输出流向前台页面展示处理结果，然后重新请求一次数据
        PrintWriter writer = response.getWriter();
        // 获取模块名称
        String contextPath = this.getServletContext().getContextPath();
        // 拼接请求路径
        String hrefPath = contextPath + "/classPageQueryServlet";
        if(0 != res) {
            writer.print("<script>alert('管理班级信息成功！'); location.href='" + hrefPath
                    + "';</script>");
        }else{
            writer.print("<script>alert('管理学生信息失败！'); location.href='" + hrefPath
                    + "';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
    }
}
